/**
 * 
 */
package com.rf.richfitwheel.admin.sys.service.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.dao.SysMsgDefMapper;
import com.rf.richfitwheel.admin.sys.model.SysMsgDef;
import com.rf.richfitwheel.admin.sys.model.SysMsgDefExample;
import com.rf.richfitwheel.admin.sys.service.SysMsgDefService;
import com.rf.richfitwheel.admin.utils.JacksonJsonUtil;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;

import net.sf.json.JSONObject;

/**
 * @author hushouquan
 *
 */
@Service("msgDefService")
public class SysMsgDefServiceImpl implements SysMsgDefService {

	@Autowired
    private SysMsgDefMapper sysMsgDefMapper;
	
	/**
	 * 分页查询
	 * @param params 分页参数
	 * @return 消息列表
	 * @throws Exception 抛出异常
	 */
	@Override
	public PageUtils queryPage(Map<String, Object> params) throws Exception {
		PageUtils pageUtils = PageUtils.getPageInfo(params);
        Page<SysMsgDef> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        String jsonStr = JSONObject.fromObject(params.get("dataForm")).toString();
        SysMsgDef sysMsgDef = (SysMsgDef) JacksonJsonUtil.jsonToObj(new SysMsgDef(), jsonStr);;
        
        SysMsgDefExample example = new SysMsgDefExample();
        SysMsgDefExample.Criteria criteria = example.createCriteria();
        if (sysMsgDef != null) {
            if (StringUtils.isNotBlank(sysMsgDef.getMsgCode())){
                criteria.andMsgCodeLike(sysMsgDef.getMsgCode());
			}
            if (StringUtils.isNotBlank(sysMsgDef.getPageType())){
                criteria.andPageTypeIn(Arrays.asList(sysMsgDef.getPageType().split(",")));
        }
        }
        example.setOrderByClause(" mod_time desc ");
        List<SysMsgDef> list = sysMsgDefMapper.selectByExample(example);
        pageUtils = new PageUtils(p);
        return pageUtils;
	}

	/**
	 * 根据id获取消息规则信息
	 * @param id
	 * @return
	 */
	@Override
	public SysMsgDef selectById(String id) throws Exception {
		return sysMsgDefMapper.selectByPrimaryKey(id);
	}

	/**
	 * 保存消息规则信息
	 * @param sysMsgDef
	 */
	@Override
	public void save(SysMsgDef sysMsgDef) {
		if (StringUtils.isBlank(sysMsgDef.getId())){
            sysMsgDef.setId(UuidUtil.get32UUID());
		}
        sysMsgDefMapper.insert(sysMsgDef);
	}

	 /**
     * 更新消息规则信息
     * @param sysMsgDef
     */
	@Override
	public void update(SysMsgDef sysMsgDef) {
		sysMsgDefMapper.updateByPrimaryKeySelective(sysMsgDef);
	}

	/**
     * 删除消息规则信息
     * @param ids
     */
	@Override
	public void delete(String[] ids) {
		for (String id : ids) {
            sysMsgDefMapper.deleteByPrimaryKey(id);
        }
		
	}

	 /**
     * 根据编号获取消息规则信息
     * @param code
     * @return
     */
	@Override
	public SysMsgDef selectSysMsgDefByCode(String code) {
		return sysMsgDefMapper.selectSysMsgDefByCode(code);
	}

	/**
     * 页面信息
     * @param id
     * @return
     */
	@Override
	public SysMsgDef pageInfo(String id) {
		return sysMsgDefMapper.pageInfo(id);
	}

}
